home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DefaultBoundedRangeModel.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  9.2 KB  |  343 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DefaultBoundedRangeModel.java    1.30 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.swing;
  16.  
  17. import javax.swing.event.*;
  18.  
  19. import java.io.Serializable;
  20.  
  21.  
  22. /**
  23.  * A generic implementation of BoundedRangeModel.
  24.  * <p>
  25.  * <strong>Warning:</strong>
  26.  * Serialized objects of this class will not be compatible with 
  27.  * future Swing releases.  The current serialization support is appropriate
  28.  * for short term storage or RMI between applications running the same
  29.  * version of Swing.  A future release of Swing will provide support for
  30.  * long term persistence.
  31.  *
  32.  * @version 1.30 08/28/98
  33.  * @author David Kloba
  34.  * @author Hans Muller
  35.  * @see BoundedRangeModel
  36.  */
  37. public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable
  38. {
  39.     /**
  40.      * Only one ChangeEvent is needed per model instance since the
  41.      * event's only (read-only) state is the source property.  The source
  42.      * of events generated here is always "this".
  43.      */
  44.     protected transient ChangeEvent changeEvent = null;
  45.  
  46.     /** The listeners waiting for model changes. */
  47.     protected EventListenerList listenerList = new EventListenerList();
  48.  
  49.     private int value = 0;
  50.     private int extent = 0;
  51.     private int min = 0;
  52.     private int max = 100;
  53.     private boolean isAdjusting = false;
  54.  
  55.  
  56.     /**
  57.      * Initializes all of the properties with default values.
  58.      * Those values are:
  59.      * <ul>
  60.      * <li><code>value</code> = 0
  61.      * <li><code>extent</code> = 0
  62.      * <li><code>minimum</code> = 0
  63.      * <li><code>maximum</code> = 100
  64.      * <li><code>adjusting</code> = false
  65.      * </ul>
  66.      */
  67.     public DefaultBoundedRangeModel() {
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Initializes value, extent, minimum and maximum. Adjusting is false.
  73.      * Throws an IllegalArgumentException if the following constraints
  74.      * aren't satisfied:
  75.      * <pre>
  76.      * min <= value <= value+extent <= max
  77.      * </pre>
  78.      */
  79.     public DefaultBoundedRangeModel(int value, int extent, int min, int max)
  80.     {
  81.         if ((max >= min) && (value >= min) && ((value + extent) <= max)) {
  82.             this.value = value;
  83.             this.extent = extent;
  84.             this.min = min;
  85.             this.max = max;
  86.         }
  87.         else {
  88.             throw new IllegalArgumentException("invalid range properties");
  89.         }
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Return the model's current value.
  95.      * @return the model's current value
  96.      * @see #setValue
  97.      * @see BoundedRangeModel#getValue
  98.      */
  99.     public int getValue() {
  100.       return value; 
  101.     }
  102.  
  103.  
  104.     /**
  105.      * Return the model's extent.
  106.      * @return the model's extent
  107.      * @see #setExtent
  108.      * @see BoundedRangeModel#getExtent
  109.      */
  110.     public int getExtent() {
  111.       return extent; 
  112.     }
  113.  
  114.  
  115.     /**
  116.      * Return the model's minimum.
  117.      * @return the model's minimum
  118.      * @see #setMinimum
  119.      * @see BoundedRangeModel#getMinimum
  120.      */
  121.     public int getMinimum() {
  122.       return min; 
  123.     }
  124.  
  125.  
  126.     /**
  127.      * Return the model's maximum.
  128.      * @return  the model's maximum
  129.      * @see #setMaximum
  130.      * @see BoundedRangeModel#getMaximum
  131.      */
  132.     public int getMaximum() {
  133.         return max; 
  134.     }
  135.  
  136.  
  137.     /** 
  138.      * Sets the current value of the model. For a slider, that
  139.      * determines where the knob appears. Ensures that the new 
  140.      * value, <I>n</I> falls within the model's constraints:
  141.      * <pre>
  142.      *     minimum <= value <= value+extent <= maximum
  143.      * </pre>
  144.      * 
  145.      * @see BoundedRangeModel#setValue
  146.      */
  147.     public void setValue(int n) {
  148.         int newValue = Math.max(n, min);
  149.         if(newValue + extent > max) {
  150.             newValue = max - extent; 
  151.         }
  152.         setRangeProperties(newValue, extent, min, max, isAdjusting);
  153.     }
  154.  
  155.  
  156.     /** 
  157.      * Sets the extent to <I>n</I> after ensuring that <I>n</I> 
  158.      * is greater than or equal to zero and falls within the model's 
  159.      * constraints:
  160.      * <pre>
  161.      *     minimum <= value <= value+extent <= maximum
  162.      * </pre>
  163.      * @see BoundedRangeModel#setExtent
  164.      */
  165.     public void setExtent(int n) {
  166.         int newExtent = Math.max(0, n);
  167.         if(value + newExtent > max) {
  168.             newExtent = max - value;
  169.         }
  170.         setRangeProperties(value, newExtent, min, max, isAdjusting);
  171.     }
  172.  
  173.  
  174.     /** 
  175.      * Sets the minimum to <I>n</I> after ensuring that <I>n</I> 
  176.      * that the other three properties obey the model's constraints:
  177.      * <pre>
  178.      *     minimum <= value <= value+extent <= maximum
  179.      * </pre>
  180.      * @see #getMinimum
  181.      * @see BoundedRangeModel#setMinimum
  182.      */
  183.     public void setMinimum(int n) {
  184.         int newMax = Math.max(n, max);
  185.         int newValue = Math.max(n, value);
  186.         int newExtent = Math.min(newMax - newValue, extent);
  187.         setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
  188.     }
  189.  
  190.  
  191.     /** 
  192.      * Sets the maximum to <I>n</I> after ensuring that <I>n</I> 
  193.      * that the other three properties obey the model's constraints:
  194.      * <pre>
  195.      *     minimum <= value <= value+extent <= maximum
  196.      * </pre>
  197.      * @see BoundedRangeModel#setMaximum
  198.      */
  199.     public void setMaximum(int n) {
  200.         int newMin = Math.min(n, min);
  201.         int newValue = Math.min(n, value);
  202.         int newExtent = Math.min(n - newValue, extent);
  203.  
  204.         setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Sets the valueIsAdjusting property.
  210.      * 
  211.      * @see #getValueIsAdjusting
  212.      * @see #setValue
  213.      * @see BoundedRangeModel#setValueIsAdjusting
  214.      */
  215.     public void setValueIsAdjusting(boolean b) {
  216.         setRangeProperties(value, extent, min, max, b);
  217.     }
  218.  
  219.  
  220.     /**
  221.      * Returns true if the value is in the process of changing
  222.      * as a result of actions being taken by the user.
  223.      *
  224.      * @return the value of the valueIsAdjusting property
  225.      * @see #setValue
  226.      * @see BoundedRangeModel#getValueIsAdjusting
  227.      */
  228.     public boolean getValueIsAdjusting() {
  229.         return isAdjusting; 
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Sets all of the BoundedRangeModel properties after forcing
  235.      * the arguments to obey the usual constraints:
  236.      * <pre>
  237.      *     minimum <= value <= value+extent <= maximum
  238.      * </pre>
  239.      * <p>
  240.      * At most, one ChangeEvent is generated.
  241.      * 
  242.      * @see BoundedRangeModel#setRangeProperties
  243.      * @see #setValue
  244.      * @see #setExtent
  245.      * @see #setMinimum
  246.      * @see #setMaximum
  247.      * @see #setValueIsAdjusting
  248.      */
  249.     public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
  250.     {
  251.         if(newMin > newMax)
  252.             newMin = newMax;
  253.         if(newValue > newMax)
  254.             newMax = newValue;
  255.         if(newValue < newMin)
  256.             newMin = newValue;
  257.         if((newExtent + newValue) > newMax)
  258.             newExtent = newMax - newValue;
  259.         if(newExtent < 0)
  260.             newExtent = 0;
  261.  
  262.         boolean isChange =
  263.             (newValue != value) ||
  264.             (newExtent != extent) ||
  265.             (newMin != min) ||
  266.             (newMax != max) ||
  267.             (adjusting != isAdjusting);
  268.  
  269.         if (isChange) {
  270.             value = newValue;
  271.             extent = newExtent;
  272.             min = newMin;
  273.             max = newMax;
  274.             isAdjusting = adjusting;
  275.  
  276.             fireStateChanged();
  277.         }
  278.     }
  279.  
  280.  
  281.     /**
  282.      * Adds a ChangeListener.  The change listeners are run each
  283.      * time any one of the Bounded Range model properties changes.
  284.      *
  285.      * @param l the ChangeListener to add
  286.      * @see #removeChangeListener
  287.      * @see BoundedRangeModel#addChangeListener
  288.      */
  289.     public void addChangeListener(ChangeListener l) {
  290.         listenerList.add(ChangeListener.class, l);
  291.     }
  292.     
  293.  
  294.     /**
  295.      * Removes a ChangeListener.
  296.      *
  297.      * @param l the ChangeListener to remove
  298.      * @see #addChangeListener
  299.      * @see BoundedRangeModel#removeChangeListener
  300.      */
  301.     public void removeChangeListener(ChangeListener l) {
  302.         listenerList.remove(ChangeListener.class, l);
  303.     }
  304.  
  305.  
  306.     /** 
  307.      * Run each ChangeListeners stateChanged() method.
  308.      * 
  309.      * @see #setRangeProperties
  310.      * @see EventListenerList
  311.      */
  312.     protected void fireStateChanged() 
  313.     {
  314.         Object[] listeners = listenerList.getListenerList();
  315.         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
  316.             if (listeners[i] == ChangeListener.class) {
  317.                 if (changeEvent == null) {
  318.                     changeEvent = new ChangeEvent(this);
  319.                 }
  320.                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  321.             }          
  322.         }
  323.     }   
  324.  
  325.     
  326.     /**
  327.      * Returns a string that displays all of the BoundedRangeModel properties.
  328.      */
  329.     public String toString()  {
  330.         String modelString =
  331.             "value=" + getValue() + ", " +
  332.             "extent=" + getExtent() + ", " +
  333.             "min=" + getMinimum() + ", " +
  334.             "max=" + getMaximum() + ", " +
  335.             "adj=" + getValueIsAdjusting();
  336.  
  337.         return getClass().getName() + "[" + modelString + "]";
  338.     }
  339.  
  340.  
  341. }
  342.  
  343.